﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MUD.Lib.Utils
{
    internal class Dice
    {
        /// <summary>
        /// Roll a number of dice with a certain number of sides, with a certain target number. Return successes.
        /// </summary>
        /// <param name="sides">Number of sides per die</param>
        /// <param name="count">Number of dice</param>
        /// <param name="target">Target number for each die</param>
        /// <returns>Number of successes (count+n for critical success and negative for critical failure)</returns>
        public static int Roll(int sides, int count, int target)
        {
            int successes = 0;
            Random roller = new Random();
            for (int i = 0; i < count; i++)
            {
                int result = (int)Math.Round((double)sides * roller.NextDouble(), 0);
                if (result > target)
                {
                    successes++;
                }
                if (result == sides)
                {
                    successes++;
                }
                if (result == 0)
                {
                    successes--;
                    successes--;
                }
            }
            return successes;
        }

        /// <summary>
        /// Roll a number of dice with default 10 sides, with a certain target number. Return successes.
        /// </summary>
        /// <param name="count">Number of dice</param>
        /// <param name="target">Target number for each die</param>
        /// <returns>Number of successes (count+n for critical success and negative for critical failure)</returns>
        public static int Roll(int count, int target)
        {
            int successes = 0;
            Random roller = new Random();
            for (int i = 0; i < count; i++)
            {
                int result = (int)Math.Round((double)10 * roller.NextDouble(), 0);
                if (result > target)
                {
                    successes++;
                }
                if (result == 10)
                {
                    successes++;
                }
                if (result == 0)
                {
                    successes--;
                    successes--;
                }
            }
            return successes;
        }

        /// <summary>
        /// Roll a number of dice with default 10 sides, no target number.
        /// </summary>
        /// <param name="count">Number of dice</param>
        /// <returns>Integer array of results</returns>
        public static int[] RollDice(int count)
        {
            int[] resultSet = new int[count];
            Random roller = new Random();
            for (int i = 0; i < count; i++)
            {
                resultSet[i] = (int)Math.Round((double)10 * roller.NextDouble(), 0);
            }
            return resultSet;
        }

        /// <summary>
        /// Roll a number of dice with a certain number of sides, no target number.
        /// </summary>
        /// <param name="sides">Number of sides per die</param>
        /// <param name="count">Number of dice</param>
        /// <returns>Integer array of results</returns>
        public static int[] RollDice(int sides, int count)
        {
            int[] resultSet = new int[count];
            Random roller = new Random();
            for (int i = 0; i < count; i++)
            {
                resultSet[i] = (int)Math.Round((double)sides * roller.NextDouble(), 0);
            }
            return resultSet;
        }
    }
}
